import { message } from "antd";
import axios from "axios";
axios.defaults.timeout = 1000 * 30;
axios.defaults.baseURL = process.env.REACT_APP_BASE_URL;
// const service = axios.create({
//   baseURL: baseURL,
//   timeout: 1000 * 30,
//   headers: {
//     "Content-Type": "application/json; charset=utf-8",
//   },
// });

axios.interceptors.request.use(
  (config) => {
    config.data = JSON.stringify(config.data);
    config.headers = {
      "Content-Type": "application/json",
      access_token: localStorage.getItem("token"),
    };
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

axios.interceptors.response.use(
  (response) => {
    console.log(response);
    const res = response.data;
    if (res.code !== 0) {
      // 业务系统返回非0code报错
      message.error(res.msg);
    }
    return res;
  },
  (error) => {
    console.log("eeeeee22222");
    let res = error.response;
    console.log(error.response);
    if (res.status === 401) {
      window.location.href = "/login";
      // this.props.history.push("/login");
      console.log("401401401");
      window.location.href = "/login";
      return Promise.reject(error);
    }
    console.log(error.response);
    let msg = error.message;
    message.error(msg.includes("timeout") ? "请求接口超时" : msg, 2);
    return Promise.reject(error);
  }
);

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((response) => {
        resolve(response.data);
      })
      .catch((error) => {
        reject(error);
      });
  });
}

export function post(url, data) {
  return new Promise((resolve, reject) => {
    axios.post(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}

// 统一接口处理返回数据
// export default function(method, url, param) {

// }

//失败提示
function msag(err) {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        message.err(err.response.data.error.details);
        break;
      case 401:
        message.err("未授权，请登录");
        break;

      case 403:
        message.err("拒绝访问");
        break;

      case 404:
        message.err("请求地址出错");
        break;

      case 408:
        message.err("请求超时");
        break;

      case 500:
        message.err("服务器内部错误");
        break;

      case 501:
        message.err("服务未实现");
        break;

      case 502:
        message.err("网关错误");
        break;

      case 503:
        message.err("服务不可用");
        break;

      case 504:
        message.err("网关超时");
        break;

      case 505:
        message.err("HTTP版本不受支持");
        break;
      default:
    }
  }
}
